/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.helper.IGraphElementHelper;
import com.cea.papyrus.core.utils.Debug;

/**
 * This class is used to add an UML element to another.
 */
public class UMLElementAddCommand  extends PapyrusCommand{

	/**
	 * The uml parent.
	 */
	protected Element newUmlParent;
	
	/**
	 * The uml parent.
	 */
	protected Element oldUmlParent;
	
	/**
	 * The uml child.
	 */
	protected Element umlChild;
	

	/**
	 * The Constructor to add the element.
	 * 
	 * @param umlparent the umlparent
	 * @param umlchild the umlchild
	 */
	public UMLElementAddCommand(Element umlparent, Element umlchild){
		super(umlchild.getClass().getInterfaces()[0]);
		this.umlChild=umlchild;
		this.newUmlParent=umlparent;
		this.oldUmlParent=this.umlChild.getOwner();
		this.setLabel("\"Add "+ umlchild.getClass().getInterfaces()[0].getSimpleName()+"\"");
	}
	
	/**
	 * Can execute.
	 * 
	 * @return <code>true</code> if the command can be executed
	 */
	public boolean canExecute() {
		if ( (newUmlParent != umlChild) && (elementFactory instanceof IGraphElementHelper) ) {
			return ((IGraphElementHelper)elementFactory).canCreateUMLElement(newUmlParent);
		} 
		return false;
	}

	/**
	 * Execute.
	 */
	public void execute() {
		Debug.debug(this, "execute "+getLabel(), channel);
		if(elementFactory instanceof IGraphElementHelper) {
			((IGraphElementHelper)elementFactory).addUMLElement(umlChild, newUmlParent);
		}
	}
	
	/**
	 * Undo.
	 */
	public void undo() {
		Debug.debug(this, "undo "+getLabel(), channel);
		if(elementFactory instanceof IGraphElementHelper) {
			((IGraphElementHelper)elementFactory).addUMLElement(umlChild, oldUmlParent);
		} 
	}
}
